//
//  LJYRecEveryDayViewController.m
//  WWLNetease
//
//  Created by 李锦耀 on 15/12/24.
//  Copyright © 2015年 Echo.Jobs. All rights reserved.
//

#import "LJYRecEveryDayViewController.h"
#import "LJYRecEveDetailViewController.h"
#import "LJYRecEveryDayModel.h"
#import "LJYRecDayTableViewCell.h"
#import "AppTools.h"
#import "MJRefresh.h"


@interface LJYRecEveryDayViewController ()<UITableViewDataSource, UITableViewDelegate, LJYRecDayTableViewCellDelegate>

@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, strong)NSMutableArray *recommendlistArray;
@property(nonatomic, strong)NSMutableArray *Array;
@property(nonatomic, strong)NSMutableArray *bannerlistArray;
@property(nonatomic, strong)UIImageView *tempImageView;
@property(nonatomic, strong)NSMutableArray *picArr;

@property(nonatomic, assign)BOOL isUpLoading;
@property(nonatomic, copy)NSString *urlStr;

@end

@implementation LJYRecEveryDayViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeFrame" object:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeFrame" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _recommendlistArray = [NSMutableArray array];
    _bannerlistArray = [NSMutableArray array];
    _Array = [[FMDBSingleton FMDBDataBase] selectReacommendName];
    self.navigationItem.title = @"每日订阅推荐";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.view.backgroundColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self creatTableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadAgain:) name:@"reloadAgain" object:nil];
//    self.navigationController.navigationBar.
//    [self.navigationController.navigationBar addObserver:self forKeyPath:@"alpha" options:NSKeyValueObservingOptionNew context:@"观察navigationBar的alpha值"];
}
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
//{
//    NSLog(@"KeyPath = %@", keyPath);
//    NSLog(@"object) = %@", object);
//    NSLog(@"change = %@", change);
//    NSLog(@"context = %@", context);
//    
//}

-(void)creatTableView
{
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) style:UITableViewStyleGrouped];
    [self.view addSubview:_myTableView];
    _myTableView.backgroundColor = [UIColor colorWithRed:0.82 green:0.81 blue:0.76 alpha:1];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(1);
        make.top.equalTo(self.view);;
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
    }];
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(upTableView)];
    [self.myTableView.mj_header beginRefreshing];
}

- (void)upTableView{
    
    self.urlStr = @"http://c.3g.163.com/nc/topicset/subscribe/recommend.html";
    
    [self addData: self.urlStr andDwon:NO];
}

- (void)addData:(NSString *)url andDwon:(BOOL)isDown
{
    [AppTools getDataWithURL:url Block:^(id result) {
        
        if (!isDown)
        {
            [self.recommendlistArray removeAllObjects];
        }
        NSDictionary *dictionary = result;
        NSArray *array = dictionary[@"recommendlist"];
        
        for (NSDictionary *dic in array)
        {
            LJYRecEveryDayModel *model = [[LJYRecEveryDayModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.recommendlistArray addObject:model];
        }
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView reloadData];
        
    } ErrorBlock:^(NSError *error) {
        
        NSLog(@"error:%@", error);
    }];
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.recommendlistArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInditify = @"cell";
    LJYRecDayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInditify];
    cell.delegate = self;
    if (cell == nil)
    {
        cell = [[LJYRecDayTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.recommend = [self.recommendlistArray objectAtIndex:indexPath.section];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LJYRecEveryDayModel *model = [self.recommendlistArray objectAtIndex:indexPath.section];
    NSString *url = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/list/%@/0-20.html", model.ename];
    LJYRecEveDetailViewController *detailVC = [[LJYRecEveDetailViewController alloc] init];
    detailVC.url = url;
    detailVC.ename = model.ename;
    detailVC.ntitle = model.tname;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

-(void)backAction
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)reloadAgain:(NSString *)stringName
{
    for (LJYRecEveryDayModel *model in self.recommendlistArray)
    {
        if ([model.tname isEqualToString:stringName])
        {
            model.isCollect = YES;
        }
    }
    [self.myTableView reloadData];
}

- (void)createViews{
    YYCycleScrollView *cycleScrollView = [[YYCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, 375 * KPropotionWidth, 200* SIZE) animationDuration:2.0];
    
    
    NSMutableArray *viewArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.picArr.count; i++) {
        self.tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375 * KPropotionWidth, 200 * SIZE)];
        
        
        NSURL *url = [NSURL URLWithString:self.picArr[i]];
        [self.tempImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage.jpg"]];
        
        self.tempImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.tempImageView.clipsToBounds = true;
        [viewArray addObject:self.tempImageView];
        
    }
    [cycleScrollView setFetchContentViewAtIndex:^UIView *(NSInteger(pageIndex)) {
        return [viewArray objectAtIndex:pageIndex];
    }];
    [cycleScrollView setTotalPagesCount:^NSInteger{
        return self.picArr.count;
    }];
    [cycleScrollView setTapActionBlock:^(NSInteger(pageIndex)) {
        
        //        [[NSNotificationCenter defaultCenter] postNotificationName:@"number" object:[NSString stringWithFormat:@"%ld", (long)pageIndex]];
        //        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld", (long)pageIndex] forKey:@"number"];
        
        
//        [self.numberDele sendNumber:[NSString stringWithFormat:@"%ld", (long)pageIndex]];
//        
    }];
    
    
//    [self.contentView addSubview:cycleScrollView];
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"offset---scroll:%f",self.myTableView.contentOffset.y);
//    UIColor *color=[UIColor redColor];
//    CGFloat offset=scrollView.contentOffset.y;
//    if (offset <= 0) {
////        self.navigationController.navigationBar.alpha = 1;
//    }
//    else if (offset > 0 && offset < 300)
//    {
//        CGFloat alpha=1-(offset/300);
////        self.navigationController.navigationBar.alpha = alpha;
////        self.navigationController.navigationBar.backgroundColor=[color colorWithAlphaComponent:alpha];
//    }
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
